package com.laikan.reader.bookfriend.activity;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;

public class SignInLuckyPopupWindow extends PopupWindow {

    private View mMenuView;
    private TextView no_lucky;//不满七天界面
    private ImageView submit;
    private RelativeLayout choujiang_layout, loading_layout, award_msg_layout;//抽奖界面，抽奖加载中，抽奖结果
    private LotteryLucky lotteryLucky;//接口
    private TextView award_msg;
    private boolean IsTouchDiss = true;

    public SignInLuckyPopupWindow(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.signinlucky, null);
        //取消按钮
        //设置按钮监听
        RelativeLayout redpackage = (RelativeLayout)mMenuView.findViewById(R.id.redpackage);
        no_lucky = (TextView)mMenuView.findViewById(R.id.no_lucky);
        submit = (ImageView)mMenuView.findViewById(R.id.submit);
        choujiang_layout = (RelativeLayout)mMenuView.findViewById(R.id.choujiang_layout);
        loading_layout = (RelativeLayout)mMenuView.findViewById(R.id.loading_layout);
        award_msg = (TextView)mMenuView.findViewById(R.id.award_msg);
        award_msg_layout = (RelativeLayout)mMenuView.findViewById(R.id.award_msg_layout);
        //   String format = String.format(context.getString(R.string.sendQuan), b.getShareReadSendQuan());
        //   tv_quan.setText(format);
        //   startShakeAnimation(redpackage);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //		this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xa0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lotteryLucky != null) {
                    lotteryLucky.lottery();
                    luckyLoading();
                }
            }
        });
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.redpackage).getTop() + 30;
                int bottom = mMenuView.findViewById(R.id.redpackage).getBottom() - 30;
                int left = mMenuView.findViewById(R.id.redpackage).getLeft() + 30;
                int right = mMenuView.findViewById(R.id.redpackage).getRight() - 30;
                int y = (int)event.getY();
                int x = (int)event.getX();
                if (IsTouchDiss) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {

                        if (y < height) {
                            dismiss();
                        }else if (y > bottom) {
                            dismiss();
                        }
                        if (x < left) {
                            dismiss();
                        }
                        if (x > right) {
                            dismiss();
                        }
                    }
                }

                return true;
            }
        });

    }

    public void setTouchEvent(boolean boo) {
        IsTouchDiss = boo;
    }

    /**
     * 设置当前能抽取奖品
     */
    public void setLucky() {
        choujiang_layout.setVisibility(View.VISIBLE);
        loading_layout.setVisibility(View.GONE);
        award_msg_layout.setVisibility(View.GONE);
        no_lucky.setVisibility(View.GONE);
    }

    /**
     * 当前没有奖品显示
     */
    public void setNoLucky() {
        choujiang_layout.setVisibility(View.GONE);
        loading_layout.setVisibility(View.GONE);
        award_msg_layout.setVisibility(View.GONE);
        no_lucky.setVisibility(View.VISIBLE);
    }

    /**
     * 加载中
     */
    private void luckyLoading() {
        choujiang_layout.setVisibility(View.GONE);
        loading_layout.setVisibility(View.VISIBLE);
        award_msg_layout.setVisibility(View.GONE);
        no_lucky.setVisibility(View.GONE);
    }

    /**
     * 获取抽奖结果
     */
    public void luckAward(String msg) {
        award_msg.setText(msg);
        choujiang_layout.setVisibility(View.GONE);
        loading_layout.setVisibility(View.GONE);
        award_msg_layout.setVisibility(View.VISIBLE);
        no_lucky.setVisibility(View.GONE);
    }

    /**
     * 抖动动画
     *
     * @param
     */
    //    private void startShakeAnimation(View v) {
    //        int pivot = Animation.RELATIVE_TO_SELF;
    //        RotateAnimation animation = new RotateAnimation(6, -6, pivot, 0.5f, pivot, 0.5f);
    //        animation.setDuration(200);
    //        animation.setRepeatCount(v.startAnimation(animation);
    //    }
    public void setLotteryLucky(LotteryLucky l) {
        this.lotteryLucky = l;
    }

    public interface LotteryLucky {

        void lottery();
    }
}
